package cn.edu.zut.internship.bean.sys;

import java.text.MessageFormat;

/**
 * @description：统一异常编码字典类
 */
public enum ErrorCodeEnum {

    /***************************************** 公共编码 start***********************************************************/
    /**
     * Success error code enum.
     */
    SUCCESS("0000", "成功"),

    /**
     * Sys error error code enum.
     */
    SYS_ERROR("9999", "系统异常"),
    NO_TOKEN("401", "您还未登录"),
    NO_ACCESS("0001", "无权访问"),

    /**
     * trainType error error code enum.
     */
    NAME_ERROR("2001","要新增的名字已存在"),
    ID_ERROR("2002","Id不能为空"),
    /**
     * file error error code enum.
     */
    FILE_TYPE_ERROR("1001","文件类型错误"),
    FILE_UPLOAD_FAIL("1002","文件上传失败"),
    FILE_NOT_EMPTY("1003","请上传文件"),
    FILE_EMPTY("1004","文件不存在"),
    FILEDIR_EMPTY("1005","文件路径错误"),
    /**
     * name not empty error code enum.
     */
    NAME_EMPTY("3001","名称不能为空"),
    /**
     * name already exists error code enum.
     */
    NAME_EXISTED("3002","该名称已存在"),
    /**
     * info not exists error code enum.
     */
    NAME_NOT_EXISTED("3002","信息不存在"),
    /**
     * Not empty error code enum.
     */
    NOT_EMPTY("1000", "参数{name}不能为空"),

    /**
     * Not empty error code enum.
     */
    QUERY_FAIL("1888", "未参加培训"),

    QUES_FAIL("1999", "问卷模板试题不能删除"),

    /**
     * Pattern error code enum.
     */
    PATTERN("1600000097", "参数{name}格式不正确"),
    LENGTH("1600000095", "参数{name}长度超长");
    private String code;

    private String msg;

    private ErrorCodeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * Gets code.
     *
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * Gets msg.
     *
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * 获取被替换{name}后的字符串
     * @param field 需要替换的值
     * @return 结果
     */
    public String getFinalMsg(String field){
        return msg.replaceFirst("\\{[^}]*\\}", field);
    }

    /**
     * 多个参数替换
     * @param field
     * @return
     */
    public String getFinalMsg(String...field) {
        if(null != field){
            return MessageFormat.format(msg, field);
        }
        return msg;
    }

    /**
     * 根据错误信息获取错误编码
     *
     * @param errorMsg 错误信息
     * @return 错误码 error code by msg
     */
    public static String getErrorCodeByMsg(String errorMsg) {
        String errorCode = "";
        ErrorCodeEnum[] errors = ErrorCodeEnum.values();
        for (ErrorCodeEnum error : errors) {
            if (errorMsg.equals(error.msg)) {
                errorCode = error.code;
                break;
            }
        }
        return errorCode;
    }

    /**
     * 根据错误码获取错误信息
     *
     * @param code 错误信息
     * @return 错误码 error code by msg
     */
    public static String getErrorMsgByCode(String code) {
        String errorMsg = "";
        ErrorCodeEnum[] errors = ErrorCodeEnum.values();
        for (ErrorCodeEnum error : errors) {
            if (code.equals(error.code)) {
                errorMsg = error.msg;
                break;
            }
        }
        return errorMsg;
    }
}

